<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:isShow}">
      <span v-if="!fullLocation" class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show='isShow'>
      <template v-if='list && list.length > 0'>
         <span @click="changeCity(item)" class="ellipsis" v-for="item in clist" :key="item.code">{{item.name}}</span>
      </template>
    <div v-else class="loading"></div>
    </div>
  </div>
</template>
<script>
import { ref, reactive, computed } from 'vue'
import { getCityList } from '@/api/product'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const target = ref(null) // 绑定ref 获取当前的标签或是组件哈
    const isShow = ref(false) // 控制配送弹窗的显示
    const list = ref([]) //  当定义一个基本类型或是引用类型的时候可直接在里面进行 进行定义
    // 选中的省市区
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    const toggle = async () => { // 点击配送地址展示与隐藏
      isShow.value = !isShow.value
      if (isShow.value) { // 打开配送地址框时调用接口
        const ret = await getCityList()
        list.value = ret
      }
      // 筛选完将此对象置空就好
      for (const key in changeResult) { // 打开关闭都将弹层关闭
        changeResult[key] = ''
      }
    }
    // 使用计算属性来改变当前显示的数据
    const clist = computed(() => {
      let result = list.value
      if (changeResult.provinceCode && changeResult.provinceName) { // 点击省级到对应的的市区
        result = result.find(item => item.code === changeResult.provinceCode).areaList
      }
      if (changeResult.cityCode && changeResult.cityName) { // 从哪对应市级筛选出县级
        result = result.find(item => item.code === changeResult.cityCode).areaList
      }
      return result
    })
    // 记录当前选择的省区地址啊
    const changeCity = (city) => {
      if (city.level === 0) { // 选择的是省级单位
        changeResult.provinceCode = city.code
        changeResult.provinceName = city.name
      } else if (city.level === 1) {
        changeResult.cityCode = city.code
        changeResult.cityName = city.name
      } else {
        changeResult.countyCode = city.code
        changeResult.countyName = city.name
        //  点击区的时候就是最后一项啦，将所有选中的传给父组件生成新的配送地址
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`
        //  将所选的新地址个父组件 覆盖原地址
        emit('change-city', changeResult)
        // 关闭弹窗
        isShow.value = false
      }
    }
    return { changeResult, target, isShow, toggle, list, changeCity, clist }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
